/*
Copyright 2010-2011 CumulusCloud
http://cumuluscloud.cc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package appmanager.client.views;

import java.util.List;

import appmanager.client.AppService;
import appmanager.client.AppServiceAsync;
import appmanager.shared.vo.Progress;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class ProgressView extends Composite {
	private static ProgressViewUiBinder uiBinder = GWT.create(ProgressViewUiBinder.class);
	interface ProgressViewUiBinder extends	UiBinder<Widget, ProgressView> {}
	private final AppServiceAsync appService = GWT.create(AppService.class);
	
	@UiField CellTable<Progress> table;
	
	private AsyncCallback<List<Progress>> retrievedProgresses = new AsyncCallback<List<Progress>>() {
		@Override
		public void onSuccess(List<Progress> result) {
			table.setRowCount(result.size());
			table.setRowData(0, result);
		}
		
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Could not connect to server");
		}
	};

	public ProgressView() {
		initWidget(uiBinder.createAndBindUi(this));
		
		// The columns
		TextColumn<Progress> nameColumn = new TextColumn<Progress>() {
			@Override
			public String getValue(Progress object) {
				return object.getName();
			}
		};
		TextColumn<Progress> progressColumn = new TextColumn<Progress>() {
			@Override
			public String getValue(Progress object) {
				return Long.toString(object.getProgress());
			}
		};
		TextColumn<Progress> statusColumn = new TextColumn<Progress>() {
			@Override
			public String getValue(Progress object) {
				return object.getStatus();
			}
		};
		TextColumn<Progress> completedColumn = new TextColumn<Progress>() {
			@Override
			public String getValue(Progress object) {
				return object.isCompleted()?"Yes":"No";
			}
		};
		
		// Add the columns
		table.addColumn(nameColumn, "Name");
		table.addColumn(progressColumn, "Progress");
		table.addColumn(statusColumn, "Status");
		table.addColumn(completedColumn, "Completed");
		
		// Create a new timer for retrieving the progresses
	    Timer t = new Timer() {
			public void run() {
				// Retrieve the list
				appService.getProgresses(retrievedProgresses);
			}
	    };
	    t.scheduleRepeating(5000);
	}
}
